package com.myyl.springcloudconsumer.controller;

import com.myyl.springcloudconsumer.client.UserClient;
import com.myyl.springcloudconsumer.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author: myyl
 * @Time: 2019/7/20 - 15:25
 * @Description:
 */

@RestController
@RequestMapping("/user")
//@DefaultProperties(defaultFallback = "defaultFallback")   feign 依赖的不是hystrix的熔断
public class UserConsumerController {


    @Autowired
    private UserClient userClient;

    // Feign
    @GetMapping("/{id}")
    public User getUserById(@PathVariable Integer id) {
        return userClient.queryById(id);
    }


    // 降级
    //@HystrixCommand(fallbackMethod = "queryByIdCallback")
    /*@HystrixCommand(
        commandProperties = {
            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "3000")
        },
        fallbackMethod = "queryByIdCallback"
    )*/
    // 熔断
   /* @HystrixCommand(
            commandProperties = {
                    @HystrixProperty(name = "circuitBreaker.requestVolumeThreshold", value = "10"),
                    @HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds", value = "10000"),
                    @HystrixProperty(name = "circuitBreaker.errorThresholdPercentage", value = "60")
            }
    )*/
    /*public String getUserById(@PathVariable Integer id) {
        if (id % 2 == 0) {
            throw new RuntimeException("");
        }
        String url = "http://provider/user/" + id;
        return restTemplate.getForObject(url, String.class);
    }*/

    public String queryByIdCallback(Integer id) {
        return "服务器正忙，请稍后再试-.-";
    }

    public String defaultFallback() {
        return "服务器正忙，请稍后再试-.----";
    }

}
